<template>
	<view class="coupon">
		<cu-custom bgColor="bg-gradual-orange">
			<block slot="content">我的优惠券</block>
		</cu-custom>
		<view class="tabbar" :style="{top: CustomBar + 'px'}">
			<view class="item" :class="{active: status == 'normal'}" @tap="setStatus('normal')">
				<text>可使用</text>
			</view>
			<view class="item" :class="{active: status == 'used'}" @tap="setStatus('used')">
				<text>已使用</text>
			</view>
			<view class="item" :class="{active: status == 'expire'}" @tap="setStatus('expire')">
				<text>已失效</text>
			</view>
		</view>
		<view class="tabbar tabbar-two" :style="{top: CustomBar + 43 + 'px'}">
			<view class="item" :class="{active: statusTwo == 'curr'}" @tap="setStatusTwo('curr')">
				<text>全场通用券</text>
			</view>
			<view class="item" :class="{active: statusTwo == 'oint'}" @tap="setStatusTwo('oint')">
				<text>门店专用券</text>
			</view>
		</view>
		<view class="list">
			<my-empty v-if="empty"></my-empty>
			<view class="" v-if="statusTwo=='curr'">
				<view class="coupons-item" v-for="(item, i) in coupons" :key="i" @tap="showModal(item)">
					<view class="item-left">
						<text class="unit">￥</text><text class="money">{{item.price}}</text>
					</view>
					<view class="item-right">
						<view class="type">抵扣券</view>
						<view class="expire">有效期至:</view>
						<view class="expire">{{item.expiretime|dataFormat}}</view>
						<text class="item-btn" v-if="status == 'normal'">立即使用&gt;&gt;</text>
						<text class="item-btn" v-if="status == 'used'">已使用</text>
						<text class="item-btn" v-if="status == 'expire'">已失效</text>
					</view>
				</view>
			</view>
			<view class="" v-if="statusTwo=='oint'">
				<view class="coupons-item-oint" v-for="(item, i) in coupons" :key="i" @tap="showModal(item)">
					<view class="coupon-oint-money">￥{{item.price}}</view>
					<view class="coupon-oint-btn" v-if="status == 'normal'">立即使用</view>
					<view class="coupon-oint-btn" v-if="status == 'used'">已使用</view>
					<view class="coupon-oint-btn" v-if="status == 'expire'">已失效</view>
					<view class="coupon-oint-title">限{{item.title}}使用</view>
					<view class="coupon-oint-time"><text class="item-standard">满{{item.use_min_price}} 减
							{{item.price}}</text><text class="item-time">{{item.expiretime|dataFormat}}</text></view>
				</view>
			</view>
		</view>
		<view class="firstTips" v-if="tipsShow">
			<image src="../../static/tips-coupon.png"></image>
			<view class="firstTips-content">
				<view class="firsttips-top">
					<view class="tips-top-title">恭喜您获得</view>
					<!-- <view class="tips-top-num">￥<text>{{tipsMsg.price}}</text></view> -->
					<view class="tips-top-num">￥<text>{{payMoney}}</text></view>
					<view class="tips-top-info">{{tipsMsg.content}}</view>
				</view>
				<view class="firsttips-content">领取后请在优惠券中心查看</view>
				<button class="closeTipsBtn" @click="changeShow">立即领取</button>
				<view class="closeBox">
					<text class="classcloseIcon cuIcon-roundclose" @click="tipsShow = false"></text>
				</view>
			</view>
		</view>
		<!-- <view class="cu-modal" style="z-index: 99;" :class="modalActive ? 'show' : ''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">优惠券转赠</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					<view class="cu-form-group">
						<input placeholder="请输入接收人UID" v-model="receiveUid"></input>
					</view>
				</view>
				<view class="cu-bar bg-white">
					<view class="action margin-0 flex-sub text-green solid-left" @tap="hideModal">取消</view>
					<view class="action margin-0 flex-sub  solid-left" @tap="give()">确定</view>
				</view>
			</view>
		</view> -->
		<view class="cu-modal" style="z-index: 99;" :class="modalActive ? 'show' : ''">
			<view class="cu-dialog">
				<view class="">
					<button class="modal-button has-border" @tap="usecoupon">使用</button>
					<button class="modal-button has-border" @tap="hideModal" open-type="share">分享</button>
					<button class="modal-button" type="default" @tap="hideModal">取消</button>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import user from "@/api/user";
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		generateCoupon
	} from "@/api/user.js"
	export default {
		data() {
			return {
				empty: false,
				modalActive: false,
				CustomBar: this.CustomBar,
				status: 'normal',
				statusTwo: 'curr',
				coupons: [],
				giveCouponId: '',
				receiveUid: '',
				scid: '',
				share: {
					path: '/pages/coupon/index',
					title: '消费红包',
					imageUrl: '/static/share_2.png'
				},
				currentItem: {},
				queryInfo: {
					giveCouponId: '',
					outPutUid: ''
				},
				price: 0,
				tipsMsg: {
					price: '',
					content: '',
				},
				tipsShow: false,
				// 传参获取的价格
				payMoney: "",
				// 传参获取的订单id
				order_sn: ""
			}
		},
		computed: {
			...mapState(['userInfo']),
		},
		onLoad(data) {
			console.log(data)
			if (data.payMoney && data.order_sn) {
				this.payMoney = data.payMoney
				this.order_sn = data.order_sn
				this.tipsShow = true
			}
			if (data.outPutUid != undefined && data.outPutUid) {
				this.queryInfo.outPutUid = data.outPutUid;
			}
			if (data.giveCouponId && data.giveCouponId != undefined) {
				this.queryInfo.giveCouponId = data.giveCouponId;
			}
			if (data.id != undefined && data.id) {
				this.scid = data.id;
				console.log(data.id)
			}
			console.log('id==' + data.id)
			console.log(this.queryInfo);
		},
		onHide() {
			this.hideModal()
		},
		onShow() {
			if (this.queryInfo.giveCouponId && this.queryInfo.outPutUid) {
				this.shareGive(this.queryInfo.giveCouponId, this.queryInfo.outPutUid)
			} else {
				this.loadData()
				this.getFirstReceive()
			}
			console.log('id===' + this.scid)
			if (this.scid) {
				this.getShopCoupon();
			}
		},
		onPullDownRefresh() {
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
			this.loadData()
		},
		onShareAppMessage(res) {
			return {
				path: this.share.path + '?giveCouponId=' + this.giveCouponId + '&outPutUid=' + this.userInfo.id,
				title: this.share.title,
				imageUrl: this.share.imageUrl
			}
		},
		methods: {
			getShopCoupon() {
				user.getShopScoupons({
						id: this.scid
					})
					.then(res => {
						if (res.code == 1) {
							this.scid == '';
							this.tipsMsg.price = res.data.price;
							this.tipsMsg.content = '获得门店专用优惠券';
							this.tipsShow = true;
							// this.loadData();
						} else {
							uni.showToast({
								title: '请不要重复领取',
								icon: 'none'
							})
							console.log(res);
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			changeShow() {
				generateCoupon({
					id: this.userInfo.id,
					purchasePrice: this.payMoney,
					order_id: this.order_sn
				}).then(res => {
					if (res.code == 1) {
						this.loadData()
						this.tipsShow = !this.tipsShow;
						uni.showToast({
							title: "领取成功"
						})
					} else {
						this.tipsShow = !this.tipsShow;
						const phone = '4008390228';
						uni.showModal({
							content: res.msg + '\n请联系客服' + phone,
							success: (res) => {
								if (res.confirm) {
									uni.makePhoneCall({
										phoneNumber: phone
									});
								}
							}
						});
					}
				})
			},
			async onTap(item) {
				if (item.status !== 'normal') {
					return;
				}
				const [actionErr, actionRes] = await uni.showActionSheet({
					itemList: ['使用', '分享'],
				});
				if (!actionRes) {
					return
				}
				if (actionRes.tapIndex === 1) {
					this.giveCouponId = item.id;
					this.showModal();
				} else {
					if (this.statusTwo == 'oint') {
						this.jumpTo('/pages/home/detail?id=' + item.spid);
					} else {
						this.jumpTo('/pages/home/index', 'tabbar');
					}
				}
			},
			async give() {
				if (!this.giveCouponId) {
					return this.toast('请选择优惠券');
				}
				if (!this.receiveUid) {
					return this.toast('请输入接收人的UID');
				}
				// 发起请求
				uni.showLoading({
					title: '转赠中~'
				})
				setTimeout(() => {
					uni.hideLoading()
				}, 5000)
				const res = await user.couponGive({
					giveCouponId: this.giveCouponId,
					receiveUid: this.receiveUid
				})
				uni.hideLoading();
				this.toast(res.msg);
				if (res.code === 0) {
					return;
				}
				if (res.code === 1) {
					this.hideModal();
					this.coupons = this.coupons.filter(item => item.id != this.giveCouponId);
					if (this.coupons.length <= 0) {
						this.empty = true;
					}
				}
			},
			async setStatus(status) {
				if (this.status === status) {
					return;
				}
				this.empty = false;
				this.coupons = [];
				this.status = status;
				this.loadData();
			},
			async setStatusTwo(status) {
				if (this.status === status) {
					return;
				}
				this.empty = false;
				this.coupons = [];
				this.statusTwo = status;
				this.loadData();
			},
			async loadData() {
				uni.showLoading({
					title: '加载中~'
				})
				setTimeout(() => {
					uni.hideLoading()
				}, 5000)

				const res = await user.coupons({
					status: this.status,
					cate_shop: this.statusTwo
				})
				uni.hideLoading();
				if (res.code === 0) {
					this.toast(res.msg);
					return;
				}
				if (res.code === 1) {
					if (res.data.coupons.length > 0) {
						this.empty = false;
						this.coupons = res.data.coupons;
					} else {
						this.empty = true;
						this.coupons = [];
					}
				}
			},
			async showModal(item) {
				if (this.status != 'normal') {
					return
				}
				this.currentItem = item;
				this.giveCouponId = item.id;
				this.modalActive = true
			},
			async hideModal() {
				this.modalActive = false
			},
			usecoupon() {
				if (this.statusTwo == 'oint') {
					this.jumpTo('/pages/home/detail?id=' + this.currentItem.spid);
				} else {
					this.jumpTo('/pages/home/index', 'tabbar');
				}
				this.hideModal()
			},
			async shareGive(giveCouponId, outPutUid) {
				const res = await user.couponGive({
					giveCouponId: giveCouponId,
					outPutUid: outPutUid
				})
				console.log(res)
				this.loadData()
				if (res.code === 0) {
					return;
				}
				if (res.code === 1) {
					this.queryInfo = {
						giveCouponId: '',
						outPutUid: ''
					}
				}
			},
			getFirstReceive() {
				user.firstReceive().then(res => {
					if (res.code == 1) {
						this.tipsMsg.price = res.data.price;
						this.tipsMsg.content = '好友转赠消费红包';
						this.tipsShow = true;
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.coupon {
		.tabbar {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			background-color: #FFFFFF;
			display: flex;
			z-index: 1;

			.item {
				flex: 1;
				text-align: center;
				font-size: 15px;

				text {
					display: inline-block;
					color: #000;
					padding: 0 5px;
					line-height: 40px;
				}

				&.active {
					text {
						color: #000;
						border-bottom: 3px solid #000;
					}
				}
			}
		}

		.tabbar-two {
			border-top: 1px solid #EEEEEE;

			&::after {
				content: "";
				position: absolute;
				left: 50%;
				height: 100%;
				width: 1px;
				background: #EEEEEE;
			}

			.item {
				flex: 1;
				text-align: center;
				font-size: 15px;

				text {
					display: inline-block;
					color: #000;
					padding: 0 5px;
					line-height: 40px;
				}

				&.active {
					text {
						color: #FF4F07;
						border-bottom: none;
					}
				}
			}
		}

		.list {
			padding-top: 80px;
			margin: 0 20px;

			.item {
				display: flex;
				align-items: center;
				margin: 10px;
				padding: 10px 15px;
				background-color: #FFFFFF;
				border-radius: 6px;

				.left {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: #FF4F07;

					.icon {
						font-size: 16px;
						font-weight: 600;
					}

					.money {
						font-size: 22px;
						font-weight: 600;
						padding-left: 2px;
					}
				}

				.expire {
					flex: 1;
					font-size: 15px;
					margin-left: 30px;
				}

				.btn {
					background-color: #FF4F07;
					color: #FFFFFF;
					padding: 2px 5px;
					border-radius: 4px;
				}
			}

			.coupons-item {
				background: linear-gradient(to right, #F9D8C9, #FFE8DD);
				padding: 10px 15px;
				margin: 10px 0;
				border-radius: 10px;
				display: flex;

				.item-left {
					flex: 0.3;
					color: #E41616;

					.unit {
						font-size: 14px;
					}

					.money {
						font-size: 40px;
						font-weight: 600;
						line-height: 50px;
					}
				}

				.item-right {
					flex: 0.7;
					color: #C3423F;
					padding-left: 10px;
					position: relative;

					.type {
						font-size: 16px;
						font-weight: 500;
					}

					.expire {
						font-size: 12px;
						margin-top: 2px;
					}

					.item-btn {
						position: absolute;
						right: 0;
						top: 40%;
						transform: translateY(-40%);
						color: #C41D1D;
						background: #fff;
						border-radius: 30px;
						padding: 4px 10px;
						font-size: 16px;
						line-height: 20px;
					}
				}
			}

			.coupons-item-oint {
				margin-top: 10px;
				background: url(../../static/coupons_bg.png) no-repeat center center;
				background-size: 100%;
				min-height: 222rpx;
				position: relative;
				margin-bottom: 10px;
				box-shadow: 2px 3px #E5E5E5;
				border-radius: 10px;

				.coupon-oint-money {
					font-size: 24px;
					color: #EF5123;
					font-weight: bold;
					padding-top: 20px;
					padding-left: 10px;
				}

				.coupon-oint-time {
					width: 100%;
					font-size: 12px;
					color: #b2b2b2;
					position: absolute;
					// right: 20px;
					bottom: 40px;
					padding: 0 20rpx;

					.item-time {
						float: right;
					}
				}

				.coupon-oint-title {
					position: absolute;
					left: 10px;
					right: 10px;
					bottom: 15px;
					font-size: 14px;
					color: #EF5123;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}

				.coupon-oint-btn {
					position: absolute;
					right: 20px;
					top: 22px;
					background: #FFE8DD;
					color: #F0562D;
					padding: 3px 6px;
					border-radius: 4px;
					font-size: 13px;
					line-height: 21px;
				}
			}
		}
	}

	.modal-button {
		color: #39B54A;

		&::after {
			border: none;
		}
	}

	.has-border {
		border-bottom: 1px solid #ddd;
		border-radius: 0;
	}

	.cu-dialog {
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		width: 100%;
		border-bottom-left-radius: 0;
		border-bottom-right-radius: 0;
	}


	.firstTips {
		width: 100vw;
		height: 100vh;
		position: fixed;
		z-index: 10;
		top: 0;
		left: 0;
		background: rgba(0, 0, 0, 0.7);
		text-align: center;

		.firstTips-content {
			width: 520rpx;
			height: 450rpx;
			top: 50%;
			margin-top: -225rpx;
			margin-left: 105rpx;
			position: absolute;
			z-index: 12;
		}

		image {
			width: 520rpx;
			height: 450rpx;
			position: relative;
			z-index: 11;
			top: 380rpx;
			// margin-left: 105rpx;
		}

		.firsttips-top {
			padding-top: 15rpx;
			font-family: 'Noto Sans SC';
			color: #f07938;

			.tips-top-title {
				font-size: 38rpx;
				margin-bottom: 10rpx;
			}

			.tips-top-num {
				font-size: 28rpx;

				text {
					font-size: 80rpx;
					font-weight: 600;
				}
			}

			.tips-top-info {
				font-size: 20rpx;
			}

		}

		.firsttips-content {
			padding-top: 50rpx;
			font-size: 20rpx;
			color: #ffffff;
			padding-bottom: 10rpx;
		}

		.closeTipsBtn {
			color: #c16403;
			font-size: 40rpx;
			width: 350rpx;
			height: 90rpx;
			line-height: 90rpx;
			border-radius: 45rpx;
			background-color: #fedf9a;
		}

		.closeBox {
			margin-top: 100rpx;

			text {
				width: 45rpx;
				height: 45rpx;
				color: #ffffff;
				font-size: 40rpx;
			}
		}
	}

	.tips-model {
		position: fixed;
		width: 100vw;
		height: 100vh;
		background-color: rgba(0, 0, 0, 0.7);
		top: 0;
		left: 0;
		z-index: 10;

		.tips-body {
			width: 450rpx;
			height: 600rpx;
			margin-left: 150rpx;
			margin-top: -300rpx;
			top: 50%;
			position: relative;
			z-index: 2;

			image {
				width: 450rpx;
			}

			.take-btn {
				position: absolute;
				width: 450rpx;
				height: 260rpx;
				bottom: 40rpx;
				left: 0;
				z-index: 3;
				color: #4c1813;
				text-align: center;
				font-weight: 'SimHei';

				button {
					font-weight: 'SimHei';
					font-size: 23rpx;
					display: inline-block;
					border-radius: 30rpx;
					color: #4c1813;
				}

				.share-btns {
					margin-right: 50rpx;
				}
			}

			.tips-title {
				font-size: 36rpx;
				margin-bottom: 30rpx;
			}

		}
	}
</style>